<?php
namespace Api\Service;

class OrderService{

	protected $error = '';
	
	/**
	 * @todo   天天特卖写入订单API 
	 * @param  int     activity_id 活动id（正常：－1；促销：－2；团购：－3）
	 * @param  int     customer_id 用户id
	 * @param  int     goods_id 商品id
	 * @param  int     merchant_id 商户id
	 * @param  int     shop_id 店铺id
	 * @param  int     goods_category_id 商品品类id
	 * @param  char    goods_name 商品名称
	 * @param  char    goods_title 商品副标题
	 * @param  char    goods_introduction 商品简介
	 * @param  double  market_price 市场价格
	 * @param  double  shop_price 商城价格
	 * @param  double  promotion_price 促销价格
	 * @param  char    goods_unit 单位
	 * @param  double  goods_deposit 定金
	 * @param  int     pic_id 封面图
	 * @param  text    goods_content 商品介绍
	 * @param  int     goods_type 类型：0:正常销售1:促销
	 * @param  int     position 位置
	 * @param  float   amount  支付金额
	 *
	 * @return array(
	 *             'status'    //1成功0失败
	 *             'info'      //成功返回订单ID，失败返回错误信息
	 *         )
	 * @author fang
	 */
	public function saleOrder($array){
		$result = array();
		$map['activity_id'] = (int)$array['activity_id'];
		$map['customer_id'] = (int)$array['customer_id'];
		$map['goods_id'] = (int)$array['goods_id'];
		$map['merchant_id'] = (int)$array['merchant_id'];
		$map['shop_id'] = (int)$array['shop_id'];
		$map['goods_category_id'] = (int)$array['goods_category_id'];
		$map['goods_category_pid'] = (int)$array['goods_category_pid'];
		$map['goods_name'] = strip_tags($array['goods_name']);
		$map['goods_title']= strip_tags($array['goods_title']);
		$map['goods_introduction'] = strip_tags($array['goods_introduction']);
		$map['market_price'] = (float)$array['market_price'];
		$map['shop_price'] = (float)$array['shop_price'];
		$map['promotion_price'] = (float)$array['promotion_price'];
		$map['goods_unit'] = strip_tags($array['goods_unit']);
		$map['goods_deposit'] = (float)$array['goods_deposit'];
		$map['pic_id'] = (int)$array['pic_id'];
		$map['goods_content'] = $array['goods_content']; //update by fanzhaohai , 这里过滤去掉了
		$map['goods_type'] = (int)$array['goods_type'];
		$map['position'] = (int)$array['position'];
		$map['amount'] = (float)$array['amount'];
		 
		$obj = D('Api/Order');
		$data = $obj->saleOrderBuild($map);
		if($data){
			$result['status'] = '1';
			$result['info'] = (int)$data;
		}else{
			$result['status'] = '0';
			$result['info'] = $obj->getError();
		}
		return $result;
	}
	
	/**
	 * @todo   根据订单id获取订单的支付状态
	 * @param  int   order_id  订单id
	 *
	 * @return array(
	 *             'status'    //1成功0失败
	 *             'info'      //成功返回订单状态0:订单创建;1:支付失败;2:已支付;失败返回错误信息
	 *         )
	 * @author fang
	 */
	public function saleOrderStatus($order_id){
		$order_id = (int)$order_id;
		$result = array();
		if($order_id <= 0){
			$result['status'] = '1';
			$result['info'] = 'order_id参数错误';
		}else{
			$order = M('order')->field('order_status,order_amount,create_time')->where(array('order_id' => $order_id))->find();
			if($order == null){
				$result['status'] = '0';
				$result['info'] = '订单不存在';
			}else{
				$result['status'] = '1';
				$result['info'] = array(
						'order_status' => (int)$order['order_status'],//0:订单创建;1:支付失败;2:已支付;
						'order_amount' => (float)$order['order_amount'],
						'create_time' => $order['create_time']
				);
			}
		}
		return $result;
	}
	
	
	/**
	 * @todo   根据订单id发送短信
	 * @param  int $order_id 订单号
	 * @param  string $code  识别码
	 *
	 * @author fzh
	 */
	public function sendOrderSms($order_id, $code){
	    $shop_order = M('ShopOrder')->field('customer_id')->where(array('order_id'=>$order_id))->find();
	    if(!empty($shop_order)){
	        $phone = getCustomerName($shop_order['customer_id']);
    	    $content = '订单号为：'.$order_id.'的订单支付成功！商品交易识别码为'.$code.'，请在提货或补交尾款时，出示该交易信息。如有疑问，请拨打服务热线4008-168-333';
    	    D('Api/Sms','Service')->sendSms($phone, $content);
	    }
	}
	/**
	 * @todo 返回最后一次错误记录
	 * @return string
	 */
	public function getError(){
		return $this->error;
	}
	
}